import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle, Circle, FancyBboxPatch, Arrow

fig, ax = plt.subplots(1, 1, figsize=(14, 10))

# 设置城市和位置
cities = {
    '城市A': {'pos': (0.2, 0.7), 'color': '#E74C3C'},
    '城市B': {'pos': (0.2, 0.3), 'color': '#3498DB'},
    '城市C': {'pos': (0.8, 0.5), 'color': '#2ECC71'}
}

# 绘制城市
for city, info in cities.items():
    x, y = info['pos']
    # 城市框
    city_box = FancyBboxPatch((x-0.08, y-0.06), 0.16, 0.12,
                              boxstyle="round,pad=0.02", facecolor=info['color'],
                              alpha=0.3, edgecolor=info['color'], linewidth=2)
    ax.add_patch(city_box)
    ax.text(x, y, city, ha='center', va='center', fontsize=12, weight='bold')

    # 机场标记
    ax.plot(x+0.05, y-0.02, '^', markersize=10, color=info['color'])
    ax.text(x+0.08, y-0.02, '机场', fontsize=9, va='center')

# 初始状态
ax.text(0.1, 0.85, '初始状态:', fontsize=11, weight='bold')
ax.text(0.2, 0.85, '• 包裹P1在城市A\n• 包裹P2在城市B', fontsize=10)

# 目标状态
ax.text(0.7, 0.85, '目标状态:', fontsize=11, weight='bold')
ax.text(0.8, 0.85, '• 包裹P1,P2都在城市C', fontsize=10)

# 绘制包裹
packages = {
    'P1': {'start': (0.2, 0.65), 'end': (0.8, 0.55), 'color': '#E74C3C'},
    'P2': {'start': (0.2, 0.35), 'end': (0.8, 0.45), 'color': '#3498DB'}
}

for pkg, info in packages.items():
    # 包裹图形
    pkg_circle = Circle(info['start'], 0.02, facecolor=info['color'], alpha=0.8)
    ax.add_patch(pkg_circle)
    ax.text(info['start'][0], info['start'][1], pkg, ha='center', va='center',
            fontsize=10, weight='bold', color='white')

    # 目标位置
    target_circle = Circle(info['end'], 0.02, facecolor=info['color'],
                           alpha=0.3, edgecolor=info['color'], linewidth=2)
    ax.add_patch(target_circle)
    ax.text(info['end'][0], info['end'][1], pkg, ha='center', va='center',
            fontsize=10, weight='bold', color=info['color'])

# 绘制运输路径
# P1的路径：城市A -> 城市A机场 -> 城市C机场 -> 城市C
ax.plot([0.2, 0.25, 0.4, 0.75, 0.8], [0.65, 0.63, 0.6, 0.52, 0.55],
        '--', color='#E74C3C', linewidth=2, alpha=0.7)
# P2的路径：城市B -> 城市C
ax.plot([0.2, 0.8], [0.35, 0.45], '-.', color='#3498DB', linewidth=2, alpha=0.7)

# 添加运输工具图标和说明
vehicles = [
    {'type': '卡车', 'pos': (0.3, 0.64), 'color': '#F39C12', 'path': '城市A内运输'},
    {'type': '飞机', 'pos': (0.55, 0.58), 'color': '#9B59B6', 'path': 'A-C空中运输'},
    {'type': '卡车', 'pos': (0.3, 0.36), 'color': '#F39C12', 'path': 'B-C陆路运输'}
]

for vehicle in vehicles:
    x, y = vehicle['pos']
    ax.text(x, y, f'🚚{vehicle["type"]}', fontsize=14, ha='center', va='center')
    ax.text(x, y-0.03, vehicle['path'], fontsize=9, ha='center', va='top',
            style='italic')

# 添加规划步骤说明
steps = [
    '1. 调度卡车将P1从城市A运到机场',
    '2. 用飞机将P1从A机场运到C机场',
    '3. 调度卡车将P2从城市B直接运到城市C',
    '4. 调度卡车将P1从C机场运到城市C'
]

for i, step in enumerate(steps):
    ax.text(0.5, 0.15 - i*0.05, step, fontsize=10, ha='center', va='center',
            bbox=dict(boxstyle="round,pad=0.3", facecolor='lightyellow', alpha=0.7))

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
ax.axis('off')
ax.set_title('物流规划案例研究示意图', fontsize=16, weight='bold', pad=20)

plt.tight_layout()
plt.savefig('logistics_case_study.png', dpi=300, bbox_inches='tight')
plt.show()